set(APP_NAME liblive2d)

# Set directory paths.
set(SDK_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../deps)
set(CORE_PATH ${SDK_ROOT_PATH}/CubismSdk/Core)
set(FRAMEWORK_PATH ${SDK_ROOT_PATH}/CubismSdk/Framework)
set(STB_PATH ${SDK_ROOT_PATH}/stb)
set(GLEW_PATH ${SDK_ROOT_PATH}/glew)
set(GLFW_PATH ${SDK_ROOT_PATH}/glfw)

project(${APP_NAME})

# Detect architecture.
if(CMAKE_EXE_LINKER_FLAGS STREQUAL "/machine:x64")
  set(ARCH x86_64)
elseif(CMAKE_EXE_LINKER_FLAGS STREQUAL "/machine:x86")
  set(ARCH x86)
else()
  message(FATAL_ERROR "[${APP_NAME}] Invalid linker flag ${CMAKE_EXE_LINKER_FLAGS}.")
endif()

# Detect compiler.
if(MSVC_VERSION MATCHES 1800)
  # Visual Studio 2013
  set(COMPILER 120)
elseif(MSVC_VERSION MATCHES 1900)
  # Visual Studio 2015
  set(COMPILER 140)
elseif(MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS 1920)
  # Visual Studio 2017
  set(COMPILER 141)
elseif(MSVC_VERSION GREATER_EQUAL 1920 AND MSVC_VERSION LESS 1930)
  # Visual Studio 2019
  set(COMPILER 142)
elseif(MSVC)
  message(FATAL_ERROR "[${APP_NAME}] Unsupported Visual C++ compiler used.")
else()
  message(FATAL_ERROR "[${APP_NAME}] Unsupported compiler used.")
endif()
# Detect core crt.
if(CORE_CRL_MD)
  set(CRT MD)
else()
  set(CRT MT)
endif()

# Add Cubism Core.
# Import as static library.
add_library(Live2DCubismCore STATIC IMPORTED)
# Find library path.
set(CORE_LIB_SUFFIX ${CORE_PATH}/lib/windows/${ARCH}/${COMPILER})
set_target_properties(Live2DCubismCore
  PROPERTIES
    IMPORTED_LOCATION_DEBUG
      ${CORE_LIB_SUFFIX}/Live2DCubismCore_${CRT}d.lib
    IMPORTED_LOCATION_RELWITHDEBINFO
      ${CORE_LIB_SUFFIX}/Live2DCubismCore_${CRT}d.lib
    IMPORTED_LOCATION_RELEASE
      ${CORE_LIB_SUFFIX}/Live2DCubismCore_${CRT}.lib
    IMPORTED_LOCATION_MINSIZEREL
      ${CORE_LIB_SUFFIX}/Live2DCubismCore_${CRT}.lib
    INTERFACE_INCLUDE_DIRECTORIES ${CORE_PATH}/include
)

# Add GLEW ,GLFW.
add_subdirectory(${GLEW_PATH}/build/cmake ${CMAKE_CURRENT_BINARY_DIR}/gl/glew)
add_subdirectory(${GLFW_PATH} ${CMAKE_CURRENT_BINARY_DIR}/gl/glfw)
set_target_properties(glfw PROPERTIES FOLDER "${APP_NAME}/deps")
set_target_properties(glew PROPERTIES FOLDER "${APP_NAME}/deps")
set_target_properties(glew_s PROPERTIES FOLDER "${APP_NAME}/deps")
set_target_properties(glewinfo PROPERTIES FOLDER "${APP_NAME}/deps")

# Specify Cubism Framework rendering.
set(FRAMEWORK_SOURCE OpenGL)
# Add Cubism Native Framework.
add_subdirectory(${FRAMEWORK_PATH} ${CMAKE_CURRENT_BINARY_DIR}/Framework)
# Add rendering definition to framework.
target_compile_definitions(Framework PUBLIC CSM_TARGET_WIN_GL)
# Add include path of GLEW to framework.
target_include_directories(Framework PUBLIC ${GLEW_PATH}/include)
# Link libraries to framework.
target_link_libraries(Framework Live2DCubismCore glew_s)

set_target_properties(Framework PROPERTIES FOLDER "${APP_NAME}/deps")
# Find opengl libraries.
find_package(OpenGL REQUIRED)

set(${APP_NAME}_SOURCES
    	src/Live2dView.cpp
    	src/Live2dTextureManager.cpp
    	src/Live2dPal.cpp
    	src/Live2dModelManager.cpp
    	src/Live2dModel.cpp
    	src/Live2dDefine.cpp
    	src/Live2dAllocator.cpp
	src/Live2dDelegate.cpp
	src/catlive2d.cpp
)

set(${APP_NAME}_HEADERS
    	include/Live2dView.hpp
    	include/Live2dTextureManager.hpp
    	include/Live2dPal.hpp
    	include/Live2dModelManager.hpp
    	include/Live2dModel.hpp
    	include/Live2dDefine.hpp
    	include/Live2dAllocator.hpp
	include/Live2dDelegate.hpp
	include/catlive2d.hpp
	)

add_library(${APP_NAME} SHARED
	${${APP_NAME}_SOURCES}
	${${APP_NAME}_HEADERS}
)
target_link_libraries(${APP_NAME}
	libcat
	Framework
  	glfw
  	${OPENGL_LIBRARIES}
)

set_target_properties(visualinfo PROPERTIES FOLDER "${APP_NAME}/deps")
set_target_properties(uninstall PROPERTIES FOLDER "${APP_NAME}/deps")
set_target_properties(${APP_NAME} PROPERTIES FOLDER "${APP_NAME}")